//
//  NSDate+ZMExtension.swift
//  IntelligentBoiler
//
//  Created by 智蚂优服 on 2020/5/20.
//  Copyright © 2020 智蚂优服. All rights reserved.
//

import Foundation
import UIKit
extension NSDate : ZMCompatible{}

extension ZM where Base == NSDate {
    
    /// 是否是今天
    /// - Returns: true or false
    func isToday() -> Bool {
        let calendar = Calendar.current
        let unit: Set<Calendar.Component> = [.day,.month,.year]
        let nowComps = calendar.dateComponents(unit, from: Date())
        let selfCmps = calendar.dateComponents(unit, from: base.self as Date)
        return (selfCmps.year == nowComps.year) && (selfCmps.month == nowComps.month) && (selfCmps.day == nowComps.day)
    }
    
    /// 是否是明天
    /// - Returns: true or false
    func isTomorrow() -> Bool {
        let calendar = Calendar.current
        let unit: Set<Calendar.Component> = [.day,.month,.year]
        let nowComps = calendar.dateComponents(unit, from: Date())
        let selfCmps = calendar.dateComponents(unit, from: base.self as Date)
        return (selfCmps.year == nowComps.year) && (selfCmps.month == nowComps.month) && (selfCmps.day == (nowComps.day ?? 0) + 1)
    }
    
    /// 是否是明天
    /// - Returns: true or false
    func isAfterTomorrow() -> Bool {
        let calendar = Calendar.current
        let unit: Set<Calendar.Component> = [.day,.month,.year]
        let nowComps = calendar.dateComponents(unit, from: Date())
        let selfCmps = calendar.dateComponents(unit, from: base.self as Date)
        return (selfCmps.year == nowComps.year) && (selfCmps.month == nowComps.month) && (selfCmps.day == (nowComps.day ?? 0) + 2)
    }
    
    /// 是否是昨天
    /// - Returns: true or false
    func isYesterday() -> Bool {
        let calendar = Calendar.current
        let unit: Set<Calendar.Component> = [.day,.month,.year]
        let nowComps = calendar.dateComponents(unit, from: Date())
        let selfCmps = calendar.dateComponents(unit, from: base.self as Date)
        if selfCmps.day == nil || nowComps.day == nil {
            return false
        }
        let count = nowComps.day! - selfCmps.day!
        return (selfCmps.year == nowComps.year) && (selfCmps.month == nowComps.month) && (count == 1)
    }
    
    
    /// 返回今天是周几
    /// - Returns: <#description#>
    func getWeekDay() -> Int {
        let calendar = Calendar.current
        if let weekday = calendar.dateComponents([.weekday], from: Date()).weekday {
            //第一天是从星期天算起，weekday在 1~7之间
            print((weekday + 5) % 7)
            return (weekday + 5) % 7
        }
        return 0
    }
    
    /// 当前时间戳，毫秒
    var timeStamp : Int {
        let timeInterval: TimeInterval = base.timeIntervalSince1970
        let timeStamp = Int(timeInterval)
        return timeStamp
    }
    
    /// 获取day
    /// - Returns: <#description#>
    func getDay() -> Int {
        let calendar = Calendar.current
        let com = calendar.dateComponents([.year, .month, .day], from: base.self as Date)
        return com.day!
    }
    
    /// 获取小时
    /// - Returns: <#description#>
    func getHour() -> Int {
        let calendar = Calendar.current
        let com = calendar.dateComponents([.year, .month, .day, .hour], from: base.self as Date)
        return com.hour!
    }
    
    /// 获取分钟
    /// - Returns: <#description#>
    func getMinute() -> Int {
        let calendar = Calendar.current
        let com = calendar.dateComponents([.year, .month, .day, .hour, .minute], from: base.self as Date)
        return com.minute!
    }
    
    
    /// 当前日期的年份
    /// - Returns: <#description#>
    func getYear() -> Int {
        let calendar = NSCalendar.current
        let com = calendar.dateComponents([.year,.month,.day], from:base.self as Date)
        return com.year!
    }
    
    /// 当前日期的月份
    /// - Returns: <#description#>
    func getMonth() -> Int {
        let calendar = NSCalendar.current
        let com = calendar.dateComponents([.year,.month,.day], from:base.self as Date)
        return com.month!
    }
    
    
    /// 获取指定格式时间
    /// - Parameter dateFormat: 格式
    func toString(dateFormat:String = "yyyy-MM-dd") -> String {
        let formatter = DateFormatter()
        formatter.locale = Locale.init(identifier: "en_US_POSIX")
        formatter.calendar = Calendar.init(identifier: Calendar.Identifier.gregorian)
        formatter.dateFormat = dateFormat
        let date = formatter.string(from: base as Date)
        return date
    }
    
    /// 获取多少天前日期
    /// - Parameter day: 多少天前
    /// - Returns: 日期
    func getAgoDay(_ day: Int) -> NSDate {
        return NSDate(timeInterval: TimeInterval(-24*60*60*day), since: base.self as Date)
    }
    
    
    /// 当前月多少天
    /// - Returns: <#description#>
    func currentMonth() -> Int {
        return NSCalendar.current.range(of: .day, in: .month, for: base.self as Date)!.count
    }
    
    
    /// 某月第一天星期几
    /// - Returns: <#description#>
    func firstWeekDayInMonthFromDate() -> Int {
        let calendar = NSCalendar.current
        var com = calendar.dateComponents([.year,.month,.day], from:base.self as Date)
        com.day = 1
        let date = calendar.date(from: com)
        let firstWeekDay = calendar.ordinality(of: .weekday, in: .weekOfMonth, for: date!)
        return firstWeekDay! - 1
    }
    
    /// 获取下个月日期
    /// - Returns: <#description#>
    func nextMonthDate() -> NSDate {
        let formater = DateFormatter()
        formater.dateFormat = "MM"
        let calendar = Calendar(identifier: .gregorian)
        var lastMonthComps = DateComponents()
        // year = 1表示1年后的时间 year = -1为1年前的日期，month day 类推
        lastMonthComps.month = 1
        return calendar.date(byAdding: lastMonthComps, to: base.self as Date)! as NSDate
    }
    
    /// 获取上个月日期
    /// - Returns: <#description#>
    func lastMonthDate() -> NSDate {
        let formater = DateFormatter()
        formater.dateFormat = "MM"
        let calendar = Calendar(identifier: .gregorian)
        var lastMonthComps = DateComponents()
        lastMonthComps.month = -1
        return calendar.date(byAdding: lastMonthComps, to: base.self as Date)! as NSDate
    }
    
    
    /// 获取多少天后日期
    /// - Parameter day: 多少天后
    /// - Returns: 日期
    func getNextDay(_ day: Int) -> NSDate {
        return NSDate(timeInterval: TimeInterval(24*60*60*day), since: base.self as Date)
    }
    
    /// 获取本周第一天
    /// - Returns: <#description#>
    func getWeekTime() -> Int {
        let calendar = Calendar.current
        let comp = calendar.dateComponents([.year, .month, .day, .weekday], from: base.self as Date)
          
        // 获取今天是周几
        let weekDay = comp.weekday
        // 获取几天是几号
        let day = comp.day
          
        // 计算当前日期和本周的星期一和星期天相差天数
        var firstDiff: Int
        // weekDay = 1;
        if (weekDay == 1) {
            firstDiff = -6;
        } else {
            firstDiff = calendar.firstWeekday - weekDay! + 1
        }
         
        // 在当前日期(去掉时分秒)基础上加上差的天数
        var firstDayComp = calendar.dateComponents([.year, .month, .day], from: base.self as Date)
        firstDayComp.day = day! + firstDiff
        let firstDayOfWeek: NSDate = calendar.date(from: firstDayComp)! as NSDate
          
        return firstDayOfWeek.zm.getDay()
    }
}
